package com.sinochem.sales.workflow.actlistener;

import com.sinochem.sales.workflow.entity.Assignee;
import com.sinochem.sales.workflow.service.AssigneeService;
import com.sinochem.sales.workflow.util.AssigneeType;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xuan😴🐶🐱🐽🌺  <a href="zhangshixuanj@163.com">联系作者</a>
 * @version Revision:v1.0,Date: 2018/5/7 16:34
 * @project auth
 * @description 流程监听器 动态注入节点办理人
 * @Modification Date: 2018/5/7 16:34  {填写修改说明}
 */
public class NodeListener implements TaskListener {

    @Resource
    private AssigneeService assigneeService;

    @Override
    public void notify(DelegateTask delegateTask) {
        //KEY
        String nodeId = delegateTask.getTaskDefinitionKey();
        Assignee assignee = new Assignee();
        assignee.setId(nodeId);
        List<Assignee> assigneeList = assigneeService.findAssignee(assignee);
        for (Assignee ag : assigneeList) {
            switch (ag.getAssigneeType()) {
                case AssigneeType.GROUP_TYPE:
                    delegateTask.addCandidateGroup(ag.getRoleId());
                    break;
                case AssigneeType.USER_TYPE:
                    delegateTask.addCandidateUser(ag.getAssignee());
                    break;
            }
        }
    }
}
